package com.frcdi.qms.web.controller.system;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.table.list.ListUtils;
import com.frcdi.qms.table.domain.Field;
import com.frcdi.qms.table.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName DataController
 * @Author Mr.Han
 * @Description //TODO controller$
 * @create 2024/5/27 13:19
 **/
@RestController
@RequestMapping("/table")
public class DataController extends BaseController {

    @Autowired
    private TableService tableService;


    // 需要过滤掉的元素列表
    List<String> itemsToFilterOut = null;

    @GetMapping("/pattern")
    public TableDataInfo findTableNames(@RequestParam String pattern) {
        List<String> list = tableService.findTableNames(pattern);
        itemsToFilterOut = Arrays.asList(
                "md_chang_history",
                "md_change_execution",
                "md_code_config",
                "md_equipment_base",
                "md_equipment_communication",
                "md_equipment_cycle",
                "md_equipment_cycle_config",
                "md_equipment_range",
                "md_equipment_tip",
                "md_equipment_type",
                "md_equipment_validation",
                "md_file",
                "md_material",
                "md_method",
                "md_method_execute",
                "md_method_project",
                "md_method_step",
                "md_step_data");
        return getDataTable(ListUtils.filterItems(list,itemsToFilterOut));
    }
    @GetMapping("/tableName")
    public TableDataInfo getFieldsByTableName(@RequestParam String tableName) {
        List<String> list = tableService.getFieldsByTableName(tableName);
        itemsToFilterOut = Arrays.asList(
                "create_by",
                "create_time",
                "del_flag",
                "description",
                "file",
                "remark",
                "update_by",
                "status",
                "update_time");
        return getDataTable(ListUtils.filterItems(list,itemsToFilterOut));
    }
    @GetMapping("/fieldData")
    public TableDataInfo getDataByTableAndField(@RequestParam String tableName, @RequestParam String fieldName) {
        List<String> list = tableService.getDataByTableAndField(new Field(tableName,fieldName,null,null,null));
        return getDataTable(ListUtils.removeDuplicates(list));
    }
}
